package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import cn.hutool.core.util.ObjectUtil;
import com.zhijian.medical.dao.local.AgentiaConsumablesCheckHistoryMapper;
import com.zhijian.medical.entity.po.AgentiaConsumablesCheckHistory;
import com.zhijian.medical.entity.vo.request.AgentiaConsumablesCheckHistorySaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.AgentiaConsumablesCheckHistorySearchVo;
import com.zhijian.medical.entity.vo.response.AgentiaConsumablesCheckHistoryVo;
import com.zhijian.medical.service.AgentiaConsumablesCheckHistoryService;
import com.zhijian.medical.util.JsonUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 试剂-分子病理室耗材验收试验记录表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-07-21 15:32:47
 */
@Slf4j
@Service
public class AgentiaConsumablesCheckHistoryServiceImpl implements AgentiaConsumablesCheckHistoryService {

    @Autowired
    private AgentiaConsumablesCheckHistoryMapper mapper;

    @Override
    public AgentiaConsumablesCheckHistory selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AgentiaConsumablesCheckHistoryVo selectVoById(Long id) {
        AgentiaConsumablesCheckHistoryVo agentiaConsumablesCheckHistoryVo = mapper.selectVoById(id);
        if (ObjectUtil.isNull(agentiaConsumablesCheckHistoryVo)) {
            return null;
        }
        String content = agentiaConsumablesCheckHistoryVo.getContent();
        if (StringUtils.isNotBlank(content)) {
            agentiaConsumablesCheckHistoryVo.setContentList(JsonUtil.toArray(content, AgentiaConsumablesCheckHistorySaveOrUpdateVo.ContentVo2.class));
        }
        return agentiaConsumablesCheckHistoryVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(AgentiaConsumablesCheckHistory agentiaConsumablesCheckHistory) {
        Date now = new Date();
        agentiaConsumablesCheckHistory.setCreateTime(now);
        agentiaConsumablesCheckHistory.setUpdateTime(now);
        return mapper.insert(agentiaConsumablesCheckHistory) > 0 ? agentiaConsumablesCheckHistory.getId() : 0L;
    }

    @Override
    public boolean save(AgentiaConsumablesCheckHistorySaveOrUpdateVo agentiaConsumablesCheckHistorySaveOrUpdateVo) {
        AgentiaConsumablesCheckHistory agentiaConsumablesCheckHistory = new AgentiaConsumablesCheckHistory();
        BeanUtils.copyProperties(agentiaConsumablesCheckHistorySaveOrUpdateVo, agentiaConsumablesCheckHistory);
        List<AgentiaConsumablesCheckHistorySaveOrUpdateVo.ContentVo2> contentList = agentiaConsumablesCheckHistorySaveOrUpdateVo.getContentList();
        if (CollectionUtil.isNotEmpty(contentList)) {
            agentiaConsumablesCheckHistory.setContent(JsonUtil.toString(contentList));
        }
        return this.insert(agentiaConsumablesCheckHistory) != 0;
    }

    @Override
    public boolean modify(AgentiaConsumablesCheckHistorySaveOrUpdateVo agentiaConsumablesCheckHistorySaveOrUpdateVo) {
        AgentiaConsumablesCheckHistory agentiaConsumablesCheckHistory = new AgentiaConsumablesCheckHistory();
        BeanUtils.copyProperties(agentiaConsumablesCheckHistorySaveOrUpdateVo, agentiaConsumablesCheckHistory);
        List<AgentiaConsumablesCheckHistorySaveOrUpdateVo.ContentVo2> contentList = agentiaConsumablesCheckHistorySaveOrUpdateVo.getContentList();
        if (CollectionUtil.isNotEmpty(contentList)) {
            agentiaConsumablesCheckHistory.setContent(JsonUtil.toString(contentList));
        }
        return this.update(agentiaConsumablesCheckHistory);
    }

    @Override
    public boolean update(AgentiaConsumablesCheckHistory agentiaConsumablesCheckHistory) {
        Date now = new Date();
        agentiaConsumablesCheckHistory.setUpdateTime(now);
        return mapper.updateByPrimaryKey(agentiaConsumablesCheckHistory) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AgentiaConsumablesCheckHistory record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(AgentiaConsumablesCheckHistorySearchVo agentiaConsumablesCheckHistorySearchVo) {
        return mapper.count(agentiaConsumablesCheckHistorySearchVo);
    }

    @Override
    public List<AgentiaConsumablesCheckHistoryVo> selectList(AgentiaConsumablesCheckHistorySearchVo agentiaConsumablesCheckHistorySearchVo) {
        List<AgentiaConsumablesCheckHistoryVo> agentiaConsumablesCheckHistoryVos = mapper.selectList(agentiaConsumablesCheckHistorySearchVo);
        if (CollectionUtil.isEmpty(agentiaConsumablesCheckHistoryVos)) {
            return Collections.emptyList();
        }
        agentiaConsumablesCheckHistoryVos.forEach(agentiaConsumablesCheckHistoryVo -> {
            String content = agentiaConsumablesCheckHistoryVo.getContent();
            if (StringUtils.isNotBlank(content)) {
                agentiaConsumablesCheckHistoryVo.setContentList(JsonUtil.toArray(content, AgentiaConsumablesCheckHistorySaveOrUpdateVo.ContentVo2.class));
            }
        });
        return agentiaConsumablesCheckHistoryVos;
    }

    @Override
    public Page<AgentiaConsumablesCheckHistoryVo> selectPage(AgentiaConsumablesCheckHistorySearchVo agentiaConsumablesCheckHistorySearchVo) {
        //先直接查总数
        int count = count(agentiaConsumablesCheckHistorySearchVo);
        //分页
        Page<AgentiaConsumablesCheckHistoryVo> page = new Page<>(true, count, agentiaConsumablesCheckHistorySearchVo.getPageNum(), agentiaConsumablesCheckHistorySearchVo.getPageSize());
        agentiaConsumablesCheckHistorySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AgentiaConsumablesCheckHistoryVo> responseVos = this.selectList(agentiaConsumablesCheckHistorySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AgentiaConsumablesCheckHistory> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    public static final String CODE_PREFIX = "FZ-SOP-C-001-03";

    @Override
    public String selectMaxCode() {
        return mapper.selectMaxCodeByDay(DateUtil.formatDate(new Date()));
    }

    @Override
    public String createNextCode() {
        String maxCode = this.selectMaxCode();
        if (StringUtils.isBlank(maxCode)) {
            return String.format("%s-%s-1", CODE_PREFIX, DateUtil.format(new Date(), FastDateFormat.getInstance("yyyyMMdd")));
        }
        String[] maxCodeArray = maxCode.split("-");
        Integer i = Integer.valueOf(maxCodeArray[maxCodeArray.length - 1]);
        i++;
        return String.format("%s-%s-%s", CODE_PREFIX, DateUtil.format(new Date(), FastDateFormat.getInstance("yyyyMMdd")), i);
    }

}